const mysql = require("mysql");
const { mysql_config } = require("./db_config");
class DB {
    constructor() {}
    query(sql, params = []) {
        return new Promise((resolve, reject) => {
            if (!sql) reject("无sql语句");
            const res = sql.match(/\?/g);
            const count = !res ? 0 : res.length;
            if (count != params.length) reject("参数长度错误");
            const conn = mysql.createConnection(mysql_config);
            conn.connect((err) => {
                if (err) reject("连接失败：" + err.message);
            });
            conn.query(sql, params, (err, res, fields) => {
                err ? reject("操作失败：" + err.message) : resolve(res, fields);
            });
            conn.end((err) => {
                if (err) reject("关闭失败：" + err.message);
            });
        });
    }
}
module.exports = new DB();